<?php

class Resume
{
    var $firstName;
    var $lastName;
    
    var $email;
    var $handPhoneCountryCode;
    var $handPhoneNo;
    var $telArea;
    var $telNo;
    var $websiteUrl;
    
    var $address1;
    var $address2;
    var $postcode;
    var $country;
    var $state;
    var $city;
    
    var $age;
    var $dateOfBirth;
    var $dobDay;
    var $dobMonth;
    var $dobYear;
    var $gender;
    var $maritalStatus;
    var $nationality;
    var $prInCountries = Array();
    
    var $preferredJobType_FullTime = false;
    var $preferredJobType_Temporary = false;
    var $preferredJobType_PartTime = false;
    var $preferredJobType_Internship = false;
    var $preferredJobType_Contract = false;
    var $expectedMonthlySalaryCurrency;
    var $expectedMonthlySalaryAmount;
    var $willingToTravel;
    var $willingToRelocate;
    
    var $additionalInfo;
    var $availability;
    var $possessOwnTransport;
    
   
    var $lastUpdated;
    
    // 1: have been working since n year, 2: fresh graduate, 3: seeking internship or part-time
    var $experienceLevel;
    var $startWorkYear;
    
    var $experiences = Array();
    var $qualifications = Array();
    var $certificate = Array();
    var $skills = Array();
    var $languages = Array();
    var $references = Array();
    
    // output as xml format in file
    function outputAsXmlFile($saveToFileName = '')
    {
        $result = Resume::outputAsXmlString();
        if($saveToFileName == '')
        {
            header ("content-type: text/xml");
            echo $result;
        }
        else
        {
            file_put_contents($saveToFileName, $result);
        }
    }
    
    // output as xml format in string
    function outputAsXmlString()
    {
        $xml = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8'?><openResume />");
        $xml->addChild("firstName", $this->firstName);
        $xml->addChild("lastName", $this->lastName);
        $xml->addChild("email", $this->email);
        $xml->addChild("handPhoneCountryCode", $this->handPhoneCountryCode);
        $xml->addChild("handPhoneNo", $this->handPhoneNo);
        $xml->addChild("telArea", $this->telArea);
        $xml->addChild("telNo", $this->telNo);
        $xml->addChild("address1", $this->address1);
        $xml->addChild("address2", $this->address2);
        $xml->addChild("postcode", $this->postcode);
        $xml->addChild("city", $this->city);
        $xml->addChild("state", $this->state);
        $xml->addChild("country", $this->country);
        $xml->addChild("age", $this->age);
        $xml->addChild("dateOfBirth", $this->dateOfBirth);
        $xml->addChild("dobDay", $this->dobDay);
        $xml->addChild("dobMonth", $this->dobMonth);
        $xml->addChild("dobYear", $this->dobYear);
        $xml->addChild("gender", $this->gender);
        $xml->addChild("maritalStatus", $this->maritalStatus);
        $xml->addChild("nationality", $this->nationality);
        if(is_array($this->prInCountries))
        {
            $xml->addChild("prInCountries");
            foreach($this->prInCountries as $prInCountry)
            {
                $xml->prInCountries->addChild("prInCountry", $prInCountry);
            }
        }
        $xml->addChild("preferredJobType_FullTime", $this->preferredJobType_FullTime);
        $xml->addChild("preferredJobType_Temporary", $this->preferredJobType_Temporary);
        $xml->addChild("preferredJobType_PartTime", $this->preferredJobType_PartTime);
        $xml->addChild("preferredJobType_Internship", $this->preferredJobType_Internship);
        $xml->addChild("preferredJobType_Contract", $this->preferredJobType_Contract);
        $xml->addChild("expectedMonthlySalaryCurrency", $this->expectedMonthlySalaryCurrency);
        $xml->addChild("expectedMonthlySalaryAmount", $this->expectedMonthlySalaryAmount);
        $xml->addChild("willingToTravel", $this->willingToTravel);
        $xml->addChild("willingToRelocate", $this->willingToRelocate);
        $xml->addChild("additionalInfo", htmlentities($this->additionalInfo));
        $xml->addChild("availability", $this->availability);
        $xml->addChild("possessOwnTransport", $this->possessOwnTransport);
        $xml->addChild("lastUpdated", $this->lastUpdated);
        $xml->addChild("experienceLevel", $this->experienceLevel);
        $xml->addChild("startWorkYear", $this->startWorkYear);
        
        // experience
        if(is_array($this->experiences))
        {
            $xmlExperiences = $xml->addChild("experiences");
            $total = count($this->experiences);
            for($i=0; $i<$total; $i++)
            {
                $xmlExperience = $xmlExperiences->addChild("experience");
                $xmlExperience->addChild("companyName", $this->experiences[$i]->companyName);
                $xmlExperience->addChild("workingPeriodFromMonth", $this->experiences[$i]->workingPeriodFromMonth);
                $xmlExperience->addChild("workingPeriodFromYear", $this->experiences[$i]->workingPeriodFromYear);
                $xmlExperience->addChild("workingPeriodToMonth", $this->experiences[$i]->workingPeriodToMonth);
                $xmlExperience->addChild("workingPeriodToYear", $this->experiences[$i]->workingPeriodToYear);
                $xmlExperience->addChild("positionLevel", $this->experiences[$i]->positionLevel);
                $xmlExperience->addChild("teamSize", $this->experiences[$i]->teamSize);
                $xmlExperience->addChild("specialization", $this->experiences[$i]->specialization);
                $xmlExperience->addChild("primaryRole", $this->experiences[$i]->primaryRole);
                $xmlExperience->addChild("industry", $this->experiences[$i]->industry);
                $xmlExperience->addChild("monthlySalaryCurrency", $this->experiences[$i]->monthlySalaryCurrency);
                $xmlExperience->addChild("monthlySalaryAmount", $this->experiences[$i]->monthlySalaryAmount);
                $xmlExperience->addChild("workDescription", htmlentities($this->experiences[$i]->workDescription));
            }
        }
        
        // qualification
        if(is_array($this->qualifications))
        {
            $xmlQualifications = $xml->addChild("qualifications");
            $total = count($this->qualifications);
            for($i=0; $i<$total; $i++)
            {
                $xmlQualification = $xmlQualifications->addChild("qualification");
                $xmlQualification->addChild("qualificationLevel", $this->qualifications[$i]->qualificationLevel);
                $xmlQualification->addChild("fieldOfStudy", $this->qualifications[$i]->fieldOfStudy);
                $xmlQualification->addChild("graduationYear", $this->qualifications[$i]->graduationYear);
                $xmlQualification->addChild("major", $this->qualifications[$i]->major);
                $xmlQualification->addChild("institution", $this->qualifications[$i]->institution);
                $xmlQualification->addChild("institutionCountry", $this->qualifications[$i]->institutionCountry);
                $xmlQualification->addChild("grade", $this->qualifications[$i]->grade);
            }
        }
        
        // certificate
        if(is_array($this->certificates))
        {
            $xmlCertificates = $xml->addChild("certificates");
            $total = count($this->certificates);
            for($i=0; $i<$total; $i++)
            {
                $xmlCertificate = $xmlCertificates->addChild("certificate");
                $xmlCertificate->addChild("title", $this->certificates[$i]->title);
                $xmlCertificate->addChild("obtainedYear", $this->certificates[$i]->obtainedYear);
            }
        }
        
        // skill
        if(is_array($this->skills))
        {
            $xmlSkills = $xml->addChild("skills");
            $total = count($this->skills);
            for($i=0; $i<$total; $i++)
            {
                $xmlSkill = $xmlSkills->addChild("skill");
                $xmlSkill->addChild("title", $this->skills[$i]->title);
                $xmlSkill->addChild("yearsOfExperience", $this->skills[$i]->yearsOfExperience);
                $xmlSkill->addChild("proficiency", $this->skills[$i]->proficiency);
            }
        }
        
        // language
        if(is_array($this->languages))
        {
            $xmlLanguages = $xml->addChild("languages");
            $total = count($this->languages);
            for($i=0; $i<$total; $i++)
            {
                $xmlLanguage = $xmlLanguages->addChild("language");
                $xmlLanguage->addChild("language", $this->languages[$i]->language);
                $xmlLanguage->addChild("spokenRating", $this->languages[$i]->spokenRating);
                $xmlLanguage->addChild("writtenRating", $this->languages[$i]->writtenRating);
            }
        }
        
        // reference
        if(is_array($this->references))
        {
            $xmlReferences = $xml->addChild("references");
            $total = count($this->references);
            for($i=0; $i<$total; $i++)
            {
                $xmlReference = $xmlReferences->addChild("reference");
                $xmlReference->addChild("title", $this->references[$i]->title);
                $xmlReference->addChild("company", $this->references[$i]->company);
                $xmlReference->addChild("phone", $this->references[$i]->phone);
                $xmlReference->addChild("email", $this->references[$i]->email);
                $xmlReference->addChild("relationship", $this->references[$i]->relationship);
            }
        }
        
        // return as xml string
        return $xml->asXML();
    }
    
    // output as plaintext format in file
    function outputAsPlaintextFile($saveToFileName = '')
    {
        $result = Resume::outputAsPlaintextString();
        if($saveToFileName == '')
        {
            header ("content-type: text/plain");
            echo $result;
        }
        else
        {
            file_put_contents($saveToFileName, $result);
        }
    }
    
    // output as plaintext format
    function outputAsPlaintextString()
    {
        $buffer = "";

        // profile
        $buffer .= "{$this->firstName}, {$this->lastName}\n";
        
        if(!empty($this->age)){$buffer .= "{$this->age}";}
        if(!empty($this->birthday)){$buffer .= " ({$this->dateOfBirth})";}
        $buffer .= ", ";
        if(!empty($this->gender)){$buffer .= "{$this->gender}";}
        $buffer .= ", ";
        if(!empty($this->maritalStatus)){$buffer .= "{$this->maritalStatus}";}
        $buffer .= ", ";
        if(!empty($this->nationality)){$buffer .= "{$this->nationality}\n";}
        
        $buffer .= "\n\n";
        
        // contact
        $buffer .= "Contact:\n\n";
        
        if(!empty($this->address1)){$buffer .= "\t{$this->address1}\n";}            
        if(!empty($this->address2)){$buffer .= "\t{$this->address2}\n";}
        if(!empty($this->city)){$buffer .= "\t{$this->city}, ";}
        if(!empty($this->postcode)){$buffer .= " {$this->postcode},\n";}
        if(!empty($this->state)){$buffer .= "\t{$this->state}, ";}
        if(!empty($this->country)){$buffer .= " {$this->country}.\n";}
        
        $buffer .= "\n";
        
        if(!empty($this->telNo)){$buffer .= "\tPhone No.: {$this->telArea}-{$this->telNo}\n";}
        if(!empty($this->handPhoneNo)){$buffer .= "\tMobile No.: {$this->handPhoneCountryCode}-{$this->handPhoneNo}\n";}
        if(!empty($this->email)){$buffer .= "\tEmail: {$this->email}\n";}
        if(!empty($this->websiteUrl)){$buffer .= "\tWebsite Url: {$this->websiteUrl}\n";}
        
        $buffer .= "\n\n";
        
        // Preferences
        $buffer .= "Job Preferences:\n\n";
        
        if(!empty($this->availability)){$buffer .= "\tAvailability: {$this->availability}\n";}
        $this->possessOwnTransport ? $buffer .= "\tPossess Own Transport: Yes\n" : $buffer .= "\tPossess Own Transport: No/Unknown\n";     
        $this->willingToTravel ? $buffer .= "\tWilling to Travel: Yes\n" : $buffer .= "\tWilling to Travel: No/Unknown\n";     
        $this->willingToRelocate ? $buffer .= "\tWilling to Relocate: Yes\n" : $buffer .= "\tWilling to Relocate: No/Unknown\n";     
        if(!empty($this->expectedMonthlySalaryAmount)){$buffer .= "\tExpected Salary: {$this->expectedMonthlySalaryCurrency} {$this->expectedMonthlySalaryAmount} per month\n";}    

        $buffer .= "\tPreferred Job Type: \n";
        if($this->preferredJobType_FullTime == true){$buffer .= "\t\t- Fulltime Job\n";}    
        if($this->preferredJobType_Temporary == true){$buffer .= "\t\t- Temporary Job\n";}    
        if($this->preferredJobType_PartTime == true){$buffer .= "\t\t- Part time Job\n";}    
        if($this->preferredJobType_Contract == true){$buffer .= "\t\t- Contract Job\n";}    
        if($this->preferredJobType_Internship == true){$buffer .= "\t\- Internship\n";}
        $buffer .= "\n";
        
        $buffer .= "\n\n";
        
        // experience
        if(is_array($this->experiences))
        {
            $buffer .= "Work Experiences:\n\n";
            $total = count($this->experiences);
            for($i=0; $i<$total; $i++)
            {
                $buffer .= "\t{$this->experiences[$i]->companyName}\n";
                if(!empty($this->experiences[$i]->positionLevel) || !empty($this->experiences[$i]->primaryRole))
                {
                    $buffer .= "\t{$this->experiences[$i]->positionLevel}, {$this->experiences[$i]->primaryRole}\n";
                }
                $buffer .= "\t{$this->experiences[$i]->workingPeriodFromMonth} {$this->experiences[$i]->workingPeriodFromYear} - {$this->experiences[$i]->workingPeriodToMonth} {$this->experiences[$i]->workingPeriodToYear}\n";
                if(!empty($this->experiences[$i]->industry)) $buffer .= "\tIndustry: {$this->experiences[$i]->industry}\n";
                if(!empty($this->experiences[$i]->specialization)) $buffer .= "\tSpecialize: {$this->experiences[$i]->specialization}\n";
                if(!empty($this->experiences[$i]->teamSize)) $buffer .= "\tTeam: {$this->experiences[$i]->teamSize}\n";
                if(!empty($this->experiences[$i]->monthlySalaryAmount))
                {
                    $buffer .= "\tSalary: {$this->experiences[$i]->monthlySalaryCurrency} {$this->experiences[$i]->monthlySalaryAmount} per month\n";
                }
                
                if(!empty($this->experiences[$i]->workDescription))
                {
                    $buffer .= "\n\t" . str_replace("\n", "\n\t", wordwrap($this->experiences[$i]->workDescription, 100)) . "\n";
                }
                $buffer .= "\n\n";
            }
            $buffer .= "\n\n";
        }
        
        // qualification
        if(is_array($this->qualifications))
        {
            $buffer .= "Qualifications/Educations:\n\n";
            $total = count($this->qualifications);
            for($i=0; $i<$total; $i++)
            {
                $buffer .= "\t".html_entity_decode($this->qualifications[$i]->institution)." {$this->qualifications[$i]->iinstitutionCountry}\n";
                $buffer .= "\t{$this->qualifications[$i]->qualificationLevel}";
                if($this->qualifications[$i]->major)
                {   
                    $buffer .= " in {$this->qualifications[$i]->major}";
                }
                if($this->qualifications[$i]->graduationYear)
                {
                    $buffer .= ", {$this->qualifications[$i]->graduationYear}";
                }
                $buffer .= "\n\tField: {$this->qualifications[$i]->fieldOfStudy}";
                if($this->qualifications[$i]->grade)
                {
                    $buffer .= ", Grade: {$this->qualifications[$i]->grade}";
                }
                
                $buffer .= "\n\n";
            }
            $buffer .= "\n\n";
        }
        
        //cert
        if(is_array($this->certs))
        {
            $buffer .= "Certs:\n\n";
            $total = count($this->certs);
            for($i=0; $i<$total; $i++)
            {
                $buffer .= "\t{$this->certs[$i]->title}";
                if(!empty($this->certs[$i]->obtainedYear))
                {
                    $buffer .= " in year {$this->certs[$i]->obtainedYear}";
                }
                $buffer .= "\n";
            }
            $buffer .= "\n\n";
        }
        
        // skills
        if(is_array($this->skills))
        {
            $buffer .= "Skills:\n\n";
            $total = count($this->skills);
            for($i=0; $i<$total; $i++)
            {
                $buffer .= "\t{$this->skills[$i]->title}, {$this->skills[$i]->proficiency} level, ".html_entity_decode($this->skills[$i]->yearsOfExperience)." year(s)\n";
            }
            $buffer .= "\n\n";
        }
        
        // languages
        if(is_array($this->languages))
        {
            $buffer .= "Languages:\n\n";
            $total = count($this->languages);
            for($i=0; $i<$total; $i++)
            {
                $buffer .= "\t{$this->languages[$i]->language}, spoken({$this->languages[$i]->spokenRating}/10), written({$this->languages[$i]->writtenRating}/10)\n";
            }
            $buffer .= "\n\n";
        }
        
        // reference
        if(is_array($this->references))
        {
            $buffer .= "References:\n\n";
            $total = count($this->references);
            for($i=0; $i<$total; $i++)
            {
                if($this->references[$i]->title)
                {
                    $buffer .= "\t{$this->references[$i]->title}";
                    
                    if(!empty($this->references[$i]->company)){$buffer .= " ({$this->references[$i]->company})";}
                    if(!empty($this->references[$i]->relationship)){$buffer .= "\n\t{$this->references[$i]->relationship}.";}
                    if(!empty($this->references[$i]->phone) || !empty($this->references[$i]->email))
                    {
                        $buffer .= "\n\t";
                        if(!empty($this->references[$i]->phone)){$buffer .= "Phone: {$this->references[$i]->phone}";}
                        if(!empty($this->references[$i]->email)){$buffer .= ", Email: {$this->references[$i]->email}";}
                    }
                    
                    $buffer .= "\n\n";
                }
            }
            $buffer .= "\n\n";
        }
        
        // additional Info
        if(!empty($this->additionalInfo))
        {
            $buffer .= "Additional Info:\n\n";
            $buffer .= "\t".str_replace("\n", "\n\t", wordwrap($this->additionalInfo, 100));
            $buffer .= "\n\n";
        }        
        
        return $buffer;
    }
    
}

class Experience
{
    var $id;
    var $companyName;
    var $workingPeriodFromMonth;
    var $workingPeriodFromYear;
    var $workingPeriodToMonth;
    var $workingPeriodToYear;
    var $positionLevel;
    var $teamSize;
    var $specialization;
    var $primaryRole;
    var $industry;
    var $monthlySalaryCurrency;
    var $monthlySalaryAmount;
    var $workDescription;
}

class Qualification
{
    var $id;
    var $qualificationLevel;
    var $fieldOfStudy;
    var $graduationYear;
    var $major;
    var $institution;
    var $institutionCountry;
    var $grade;
}

class Certificate
{
    var $id;
    var $title;
    var $obtainedYear;
}

class Skill
{
    var $id;
    var $title;
    var $yearsOfExperience;
    var $proficiency;
}

class Language
{
    var $id;
    var $language;
    // per 10
    var $spokenRating;
    // per 10
    var $writtenRating;
}

class Reference
{
    var $id;
    var $title;
    var $company;
    var $phone;
    var $email;
    var $relationship;
}